package kp.rpc.loadbalancer.impl;

import kp.rpc.loadbalancer.LoadBalancer;
import kp.rpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author 86187
 * @description: 随机负载均衡器
 * @date: 2024/6/7 12:53
 */

public class RandomLoadBalancer implements LoadBalancer {
    private final Random random = new Random();

    @Override
    public ServiceMetaInfo select(Map<String, Object> requestParams, List<ServiceMetaInfo> serviceMetaInfoList) {
        if (serviceMetaInfoList == null || serviceMetaInfoList.isEmpty()) {
            return LoadBalancer.super.select(requestParams, serviceMetaInfoList);
        }
        return serviceMetaInfoList.get(random.nextInt(serviceMetaInfoList.size()));
    }
}
